<template>
  <CommonLayout default-active-menu="/interview-history">
    <div class="interview-history">
      <div class="page-header">
        <h2 class="page-title">面试历史记录</h2>
        <div class="header-actions">
          <el-button type="primary" plain @click="exportHistory">
            <el-icon><Download /></el-icon>导出记录
          </el-button>
          <el-button type="success" @click="analyzePerformance">
            <el-icon><DataAnalysis /></el-icon>绩效分析
          </el-button>
        </div>
      </div>
      
      <!-- 筛选栏 -->
          <!--
      <el-card class="filter-card">
        <el-form :inline="true" :model="filterForm" class="filter-form">
          <el-form-item label="时间范围">
            <el-date-picker
              v-model="filterForm.dateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
          <el-form-item label="面试职位">
            <el-select v-model="filterForm.position" placeholder="请选择职位" clearable>
              <el-option label="全部职位" value="" />
              <el-option label="前端开发" value="frontend" />
              <el-option label="后端开发" value="backend" />
              <el-option label="全栈开发" value="fullstack" />
              <el-option label="产品经理" value="pm" />
              <el-option label="UI设计师" value="ui" />
            </el-select>
          </el-form-item>
          <el-form-item label="面试结果">
            <el-select v-model="filterForm.result" placeholder="选择结果" clearable>
              <el-option label="全部结果" value="" />
              <el-option label="通过" value="pass" />
              <el-option label="未通过" value="fail" />
              <el-option label="待定" value="pending" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleFilter">筛选</el-button>
            <el-button @click="resetFilter">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
          -->
      
      <!-- 数据统计卡片 -->
      <div class="statistics-cards">
        <el-card class="stat-card">
          <template #header>
            <div class="card-header">
              <span>总面试次数</span>
              <el-tooltip content="包含所有已完成的面试">
                <el-icon><InfoFilled /></el-icon>
              </el-tooltip>
            </div>
          </template>
          <div class="stat-value">{{ statistics.totalCount }}</div>
        </el-card>
        
        <el-card class="stat-card">
          <template #header>
            <div class="card-header">
              <span>通过率</span>
              <el-tooltip content="通过面试的百分比">
                <el-icon><InfoFilled /></el-icon>
              </el-tooltip>
            </div>
          </template>
          <div class="stat-value">{{ statistics.passRate }}%</div>
        </el-card>
        
        <el-card class="stat-card">
          <template #header>
            <div class="card-header">
              <span>平均得分</span>
              <el-tooltip content="所有面试的平均得分">
                <el-icon><InfoFilled /></el-icon>
              </el-tooltip>
            </div>
          </template>
          <div class="stat-value">{{ statistics.averageScore }}</div>
        </el-card>
        
        <el-card class="stat-card">
          <template #header>
            <div class="card-header">
              <span>最高得分</span>
              <el-tooltip content="历史最高得分">
                <el-icon><InfoFilled /></el-icon>
              </el-tooltip>
            </div>
          </template>
          <div class="stat-value">{{ statistics.highestScore }}</div>
        </el-card>
      </div>
      
      <!-- 面试历史列表 -->
      <el-table
        v-loading="loading"
        :data="historyList"
        style="width: 100%"
        :default-sort="{ prop: 'created_at', order: 'descending' }"
      >
        <el-table-column prop="created_at" label="面试日期" sortable width="190" >
          <template #default="scope">
            {{ formatDate(scope.row.created_at) }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="公司名称" width="190" />
        <el-table-column prop="job_title" label="面试职位" width="150" />
        <el-table-column prop="interview_format" label="面试类型" width="100">
          <template #default="scope">
            <el-tag :type="getTypeTag(scope.row.interview_format)">{{ scope.row.interview_format }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="total_score" label="得分" width="70" sortable>
          <template #default="scope">
            <span :class="getScoreClass(scope.row.total_score)">{{ scope.row.total_score }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="result" label="结果" width="90">
          <template #default="scope">
            <el-tag :type="getResultTag(scope.row.total_score)">{{ getResultText(scope.row.total_score) }}</el-tag>
          </template>
        </el-table-column>
        <!--        <el-table-column label="操作" width="180" fixed="right">
          <template #default="scope">
            <el-button size="small" @click="viewDetail(scope.row.id)">查看详情</el-button>
            <el-button size="small" type="primary" @click="reviewRecording(scope.row.id)">回放录像</el-button>
          </template>
        </el-table-column>
        -->

      </el-table>
      
      <!-- 分页 -->
      <!-- <div class="pagination-container">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :page-size="pageSize"
          :current-page="currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div> -->
    </div>
  </CommonLayout>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { Download, DataAnalysis, InfoFilled } from '@element-plus/icons-vue';
import CommonLayout from '@/components/CommonLayout.vue';
import { getHistoryInfo } from '@/api/home';

import { format } from 'date-fns';
const router = useRouter();
const loading = ref(false);

// 分页相关
const total = ref(0);
const pageSize = ref(10);
const currentPage = ref(1);

// 筛选表单
const filterForm = reactive({
  dateRange: [],
  position: '',
  result: ''
});

const formatDate = (date: string) => {
  return format(new Date(date), 'yyyy-MM-dd');
};

// 数据统计
const statistics = reactive({
  totalCount: 0,
  passRate: 0,
  averageScore: 0,
  highestScore: 0
});

// 模拟的面试历史数据
const historyList = ref();

// 获取面试类型标签样式
const getTypeTag = (type: string) => {
  switch (type) {
    case 'online': return '线上面试';
    case 'HR面': return 'success';
    case '作品面': return 'warning';
    default: return 'info';
  }
};

// 获取结果标签样式
const getResultTag = (result: number) => {

  if(result>=60){
    return 'success'
  }
  return 'danger'
};

// 获取结果文本
const getResultText = (result: number) => {
  if(result>=60){
    return '通过'
  }
  return '未通过'
};

// 获取分数样式
const getScoreClass = (score: number) => {
  if (score >= 90) return 'score-excellent';
  if (score >= 80) return 'score-good';
  if (score >= 70) return 'score-average';
  return 'score-poor';
};

// 处理筛选
const handleFilter = () => {
  loading.value = true;
  // 模拟请求
  setTimeout(() => {
    loading.value = false;
    // 实际应用中这里应该是API调用
    ElMessage.success('筛选成功');
  }, 500);
};

// 重置筛选
const resetFilter = () => {
  filterForm.dateRange = [];
  filterForm.position = '';
  filterForm.result = '';
  handleFilter();
};

// 查看详情
const viewDetail = (id: number) => {
  router.push(`/interview-detail/${id}`);
};

// 回放录像
const reviewRecording = (id: number) => {
  router.push(`/interview-recording/${id}`);
};

// 导出历史记录
const exportHistory = () => {
  ElMessage.success('历史记录导出成功');
  // 实际应用中应该是调用API下载CSV/Excel文件
};

// 绩效分析
const analyzePerformance = () => {
  router.push('/performance-analysis');
};

// 分页大小变化
const handleSizeChange = (size: number) => {
  pageSize.value = size;
  // 获取数据
};

// 页码变化
const handleCurrentChange = (page: number) => {
  currentPage.value = page;
  // 获取数据
};

onMounted( async() => {
  // 获取面试历史数据
  loading.value = true;
  setTimeout(() => {
    loading.value = false;
    // 实际应用中这里应该是API调用
  }, 500);
  let userId = localStorage.getItem("userid")
  const response = await getHistoryInfo(userId)
  historyList.value = response.history
  statistics.averageScore = response.stats.average_score
  statistics.highestScore = response.stats.highest_score
  statistics.passRate = response.stats.pass_rate
  statistics.totalCount = response.stats.total_interviews
  
});
</script>

<style scoped>
.interview-history {
  padding-bottom: 20px;
  width: 100%;
  height: 100%;
  flex-direction: column;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-title {
  margin: 0;
  font-size: 20px;
  color: #333;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.filter-card {
  margin-bottom: 20px;
  width: 100%;
}

.filter-form {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.statistics-cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
  margin-bottom: 20px;
}

.stat-card {
  text-align: center;
  height: 100%;
}

.card-header {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 5px;
}

.stat-value {
  font-size: 28px;
  font-weight: bold;
  color: #409EFF;
}

/* 让表格占据剩余空间 */
.el-table {
  flex: 1;
  min-height: 0; /* 解决flex子元素无法收缩的问题 */
}

.score-excellent {
  color: #67C23A;
  font-weight: bold;
}

.score-good {
  color: #409EFF;
  font-weight: bold;
}

.score-average {
  color: #E6A23C;
  font-weight: bold;
}

.score-poor {
  color: #F56C6C;
  font-weight: bold;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

/* 媒体查询适配不同屏幕尺寸 */
@media screen and (max-width: 1200px) {
  .statistics-cards {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media screen and (max-width: 768px) {
  .header-actions {
    flex-direction: column;
    gap: 5px;
  }
  
  .filter-form .el-form-item {
    margin-right: 0;
    margin-bottom: 10px;
    width: 100%;
  }
  
  .filter-form .el-date-editor.el-input,
  .filter-form .el-date-editor.el-input__inner,
  .filter-form .el-select {
    width: 100%;
  }
}

@media screen and (max-width: 576px) {
  .statistics-cards {
    grid-template-columns: 1fr;
  }
}
</style> 